//
//  MJ_DetailsViewController.m
//  NewClothesForEveryDay
//
//  Created by dllo on 15/7/15.
//  Copyright (c) 2015年 蓝天. All rights reserved.
//

#import "MJ_DetailsViewController.h"
#import "AFNetworkHandle.h"
#import "UIImageView+WebCache.h"
#import "MJTwoPageModel.h"
#import "MJTwoPicModel.h"
#import "MJ_DetailsHeaderView.h"
#import "BaseHeader.h"
#import "MJ_DetailsCell.h"
#import "MJ_PhotoViewController.h"

@interface MJ_DetailsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *myArr;
@property (nonatomic, retain) NSMutableArray *picArr;
@property (nonatomic, retain) MJ_DetailsHeaderView *myHeaderView;
@property (nonatomic, retain) UITableView *tableView;

@end

@implementation MJ_DetailsViewController

- (void)dealloc
{
    [_myId release];
    [_myArr release];
    [_picArr release];
    [_myHeaderView release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];//
    [self getData];
    
}
- (void)getData
{
    NSString *url = [NSString stringWithFormat:@"http://mrosevision.sinaapp.com/mfashion_api_v200.php?ac=getarticleinfo&id=%@", self.myId];
    [AFNetworkHandle GetwithAFNByUrl:url completion:^(id result) {
        NSArray *tempArr = [result objectForKey:@"lists"];
        NSArray *temp = [result objectForKey:@"lists"];
        NSArray *tempPic = [[temp firstObject] objectForKey:@"imgs"];
        self.myArr = [NSMutableArray array];
        self.picArr = [NSMutableArray array];
        self.myArr = [MJTwoPageModel arrayWithModelByArray:tempArr];
        self.picArr = [MJTwoPicModel arrayWithModelByArray:tempPic];
        
        
        [self makeTableView];
    }];
}

//- (void)makeHeaderView
//{
//    self.myHeaderView = [[MJ_DetailsHeaderView alloc] init];
//    self.myHeaderView.title = [[self.myArr firstObject] title];
//    self.myHeaderView.content = [[self.myArr firstObject] content];
//    
//    self.myHeaderView.frame = CGRectMake(0, 64, ScreenWidth, [self.myHeaderView heightWithTitle] + [self.myHeaderView heightWithContent]);
//    
//    
//    self.view.backgroundColor = [UIColor whiteColor];
//    
//    
//}

- (void)makeTableView
{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.backgroundColor = [UIColor blueColor];
    
    self.myHeaderView = [[MJ_DetailsHeaderView alloc] init];
    self.myHeaderView.title = [[self.myArr firstObject] title];
    self.myHeaderView.content = [[self.myArr firstObject] content];
    self.myHeaderView.frame = CGRectMake(0, 0, ScreenWidth, [self.myHeaderView heightWithTitle] + [self.myHeaderView heightWithContent] + 50);
    
    self.tableView.separatorColor = [UIColor clearColor];
    
    self.tableView.tableHeaderView = self.myHeaderView;
    /**
     *  尾部视图空间
     */
    UIView *p = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)] autorelease];
    self.tableView.tableFooterView = p;
    
    
//    self.tableView.tableFooterView = self.myHeaderView;
    
    

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.picArr.count;
}

#pragma mark - 每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
#pragma mark - 每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MJ_DetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LTAAA"];
    if (cell == nil) {
        cell = [[MJ_DetailsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LTAAA"];
    }
    cell.picModel = self.picArr[indexPath.section];
    MJTwoPicModel *p = [[MJTwoPicModel alloc] init];
    p = self.picArr[indexPath.section];
    cell.picWidth = p.width;
    cell.picHeight = p.height;
//    if (p.comment == nil) {
//        [cell.myLabel removeFromSuperview];
//    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MJ_PhotoViewController *VC = [[[MJ_PhotoViewController alloc] init] autorelease];
    VC.picArr = [NSArray array];
    VC.picArr = self.picArr;
    VC.nowPicModel = self.picArr[indexPath.section];
    
    
    [self.navigationController pushViewController:VC animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MJTwoPicModel *p = self.picArr[indexPath.section];
    /**
     *  图片按比例缩放  (按照屏幕宽缩放)   图片宽 / 图片高    *    屏幕宽
     */
    return  [p.height floatValue] * ScreenWidth / [p.width floatValue];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}


@end
